﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemDAO.DAO;

namespace NangLuongTieuDiemBUS.BUS
{
    public class LoaiSanPhamBUS
    {
        #region "[Insert]"
        public int Insert(LoaiSanPhamDTO lspDto)
        {
            // Kiem tra Business Rule



            // Them
            int IDResult = -1;
            try
            {
                IDResult = LoaiSanPhamDAO.Insert(lspDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Insert", ex);
            }
            return IDResult;

        }
        #endregion

        #region "[Delete]"
        public bool Delete(int ID)
        {
            // Kiem tra Business Rule



            // Xoa
            bool IDResult = false;
            try
            {
                IDResult = LoaiSanPhamDAO.Delete(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Delete", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Update]"
        public bool Update(LoaiSanPhamDTO lspDto)
        {
            // Kiem tra Business Rule



            // Sua
            bool IDResult = false;
            try
            {
                IDResult = LoaiSanPhamDAO.Update(lspDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Update", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Get all]"
        public List<LoaiSanPhamDTO> GetAll()
        {
            List<LoaiSanPhamDTO> dslspDto = new List<LoaiSanPhamDTO>();
            try
            {
                dslspDto = LoaiSanPhamDAO.GetAll();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAll", ex);
            }
            return dslspDto;
        }
        #endregion

        #region "[SelectByPrimaryKey]"
        public LoaiSanPhamDTO SelectByPrimaryKey(int ID)
        {
            LoaiSanPhamDTO lspDto = new LoaiSanPhamDTO();
            try
            {
                lspDto = LoaiSanPhamDAO.SelectByPrimaryKey(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByPrimaryKey", ex);
            }
            return lspDto;
        }
        #endregion

        #region "[SelectByLoaiSanPhamChinhID]"
        public List<LoaiSanPhamDTO> SelectByLoaiSanPhamChinhID(int ID)
        {
            List<LoaiSanPhamDTO> ds_lspDto = new List<LoaiSanPhamDTO>();
            try
            {
                ds_lspDto = LoaiSanPhamDAO.SelectByLoaiSanPhamChinhID(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByLoaiSanPhamChinhID", ex);
            }
            return ds_lspDto;
        }
        #endregion

        #region "[SelectByName]"
        /// <summary>
        /// Lấy thông tin từ tên loại sản phẩm
        /// </summary>
        /// <param name="_strNameProductType">tên loại sản phẩm</param>
        /// <returns></returns>
        public LoaiSanPhamDTO SelectByName(string _strNameProductType)
        {
            LoaiSanPhamDTO objResult = new LoaiSanPhamDTO();
            try {
                objResult = LoaiSanPhamDAO.SelectByName(_strNameProductType);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByName", ex);
            }
            return objResult;
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "LoaiSanPhamBUS_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            LoiHeThongDAO.Insert(lhtDto);
        }
        #endregion
    }
}